package cn.bdqn.t354.service.impl;

import cn.bdqn.t354.config.shiro.MyShiroRealm;
import cn.bdqn.t354.mapper.RoleMapper;
import cn.bdqn.t354.pojo.Right;
import cn.bdqn.t354.pojo.Role;
import cn.bdqn.t354.pojo.RoleRight;
import cn.bdqn.t354.service.RightService;
import cn.bdqn.t354.service.RoleRightService;
import cn.bdqn.t354.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    RoleRightService roleRightService;
    @Resource
    RightService rightService;

    @Override
    public List<Role> findAllRoles() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<Role> getRolesByPage(String roleName, Integer pageNo, Integer
            pageSize) {
        //组装分页条件
        Page<Role> page=new Page<>(pageNo,pageSize);
        //组装过滤条件 动态sql
        QueryWrapper<Role> qw=new QueryWrapper();
        qw.like(roleName!=null&&!"".equals(roleName),"role_name",roleName);
        //调用分页方法
        IPage<Role> rolePage=roleMapper.selectPage(page, qw);
        //返回分页对象
        return rolePage;
    }

    @Override
    public void addRole(Role role, String[] rightCodes) {
        //1.保存角色
        roleMapper.insert(role);

        //2.保存关联的权限
        if(rightCodes!=null && rightCodes.length>0){
            for (String rightCode : rightCodes) {
                RoleRight roleRight=new RoleRight();
                roleRight.setRfRoleId(role.getRoleId());
                roleRight.setRfRightCode(rightCode);

                roleRightService.addRoleRight(roleRight);
            }
        }
    }

    @Override
    public Role getRole(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void updateRole(Role role, String[] rightCodes) {
        //1.修改角色
        roleMapper.updateById(role);

        //2.删除角色关联的所有权限
        roleRightService.deleteByRoleId(role.getRoleId());

        //3.重新保存角色关联的权限
        if(rightCodes!=null && rightCodes.length>0){
            for (String rightCode : rightCodes) {
                RoleRight roleRight=new RoleRight();
                roleRight.setRfRoleId(role.getRoleId());
                roleRight.setRfRightCode(rightCode);

                roleRightService.addRoleRight(roleRight);
            }
        }

        //4.刷新所有用户的权限缓存
       /* RealmSecurityManager securityManager =
                (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm myShiroRealm =
                (MyShiroRealm)securityManager.getRealms().iterator().next();
        myShiroRealm.clearAllAuthenticationInfoCache();*/
    }

    @Override
    public void deleteRole(Long roleId) {
        //1. 删除角色关联的权限
        roleRightService.deleteByRoleId(roleId);
        //2. 删除角色
        roleMapper.deleteById(roleId);
    }

    @Override
    public Role getRoleByUsrId(Long usrId) {
        return roleMapper.getRoleByUsrId(usrId);
    }
}
